<template>
  <div class="ele-body">
    <el-card shadow="never" body-style="padding:22px 22px 0 22px;">
      <!-- 搜索表单 -->
      <el-form
        ref="searchForm"
        :model="table.where"
        label-width="120px"
        @keyup.enter.native="$refs.table.reload({page:{page:1,limit:10}})"
        @submit.native.prevent
      >
        <el-row>
          <el-col :md="8" :sm="12">
            <el-form-item label="关键字:" prop="name">
              <el-input
                v-model="table.where.key"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :md="8" :sm="12">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="$refs.table.reload({page:{page:1,limit:10}})"
              >查询
              </el-button>
              <el-button
                @click="(table.where = {}) && $refs.table.reload()"
              >重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="never">
      <!-- 表格工具栏 -->
      <div class="ele-table-tool">
        <div class="ele-table-tool-title">漏洞列表</div>
        <div class="ele-table-tool-right">
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="dialogTitle='创建漏洞' ;showEdit = true"
          >添加
          </el-button>
        </div>
      </div>
      <!-- 数据表格 -->
      <ele-data-table
        ref="table"
        :config="table"
        :choose.sync="choose"
        highlight-current-row
      >
        <template slot-scope="{}">
          <el-table-column
            prop="name"
            label="漏洞名称"
            show-overflow-tooltip
            min-width="110"
          />
          <el-table-column
            prop="asset"
            label="所属资产"
            show-overflow-tooltip
            min-width="135"
          >
            <template slot-scope="{ row }">{{ row.asset.name }}</template>
          </el-table-column>
          <el-table-column
            prop="level"
            label="漏洞等级"
            show-overflow-tooltip
            min-width="135"
          >
            <template slot-scope="{ row }">{{ row.level.name }}</template>
          </el-table-column>
          <el-table-column
            prop="source"
            label="漏洞来源"
            show-overflow-tooltip
            min-width="135"
          >
            <template slot-scope="{ row }">{{ row.source.name }}</template>
          </el-table-column>
          <el-table-column
            prop="type"
            label="漏洞类型"
            show-overflow-tooltip
            min-width="135"
          >
            <template slot-scope="{ row }">{{ row.type.name }}</template>
          </el-table-column>
          <el-table-column
            prop="create_time"
            label="创建时间"
            show-overflow-tooltip
            min-width="135"
          >
            <template slot-scope="{ row }">{{
              row.add_time | moment
            }}</template>
          </el-table-column>
          <el-table-column
            prop="update_time"
            label="更新时间"
            show-overflow-tooltip
            min-width="135"
          >
            <template slot-scope="{ row }">{{
              row.update_time | moment
            }}</template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            min-width="160"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-link
                icon="el-icon-edit"
                type="primary"
                :underline="false"
                @click="edit(scope.row)"
              >配置</el-link>

              <el-popconfirm
                title="您确定删除吗？"
                @confirm="del(scope.row.id)"
              >
                <el-link
                  slot="reference"
                  icon="el-icon-delete"
                  type="primary"
                  :underline="false"
                >删除</el-link>
              </el-popconfirm>

              <el-link
                icon="el-icon-finished"
                type="primary"
                :underline="false"
                @click="jump(scope.row)"
              >详情
              </el-link>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="showEdit"
      :close-on-click-modal="false"
      width="80%"
      :lock-scroll="false"
      @closed="cleanForm"
      @opened="transData"
    >
      <vuln-form ref="vulnForm" :data="vulnDetial" @changeLoading="changeLoading" @postSucceed="postSucceed" />
      <div slot="footer">
        <el-button @click="showEdit = false">取消</el-button>
        <el-button
          type="primary"
          :loading="editLoading"
          @click="save"
        >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import vulnForm from '../components/vulnForm'
import { getTicket } from '@/api/workflow'
export default {
  name: 'VulnList',
  filters: {
    moment: function(value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  components: {
    vulnForm
  },
  data() {
    return {
      table: { // 表格配置
        url: '/vuln/vuln/list/',
        where: {}
      }, // 编辑弹窗数据
      choose: [],
      showEdit: false, // 是否显示编辑弹窗
      editLoading: false, // 编辑表单加载状态
      assetType: [], // 资产类型
      loading: false,
      managerLoading: false,
      assetOption: [],
      userOption: [],
      dialogTitle: '创建漏洞',
      editForm: {},
      choosed: {},
      vulnDetial: {}
    }
  },
  methods: {
    /* 清空选择 */
    clearChoose() {
      this.$refs.table.clearSelection()
    },
    /* 保存并提交表单 */
    save() {
      this.$refs.vulnForm.save()
    },
    /* 编辑 */
    edit(row) {
      this.dialogTitle = '更新漏洞信息'
      this.choosed = row
      getTicket(row.ticket_id).then(res => {
        this.vulnDetial = res.data.data.info
      })
      this.showEdit = true
    },
    /* 跳转 */
    jump(row) {
      this.$router.push(`/vuln/detial/${row.ticket_id}`)
    },
    /* 清空vulnform */
    cleanForm() {
      this.$refs.vulnForm.cleanForm()
    },
    /* 修改loading */
    changeLoading(status) {
      this.editLoading = status
    },
    /* 监听子页面提交成功 */
    postSucceed() {
      this.$refs.table.reload()
      this.showEdit = false
    },
    /* 打开弹窗回调 */
    transData() {
      this.editForm = this.choosed
    }
  }
}
</script>

<style scoped>
.el-link {
  margin: 0 2px;
}
</style>
